package com.cntaiping.royal.bass.system.util;

import com.cntaiping.royal.bass.system.common.SpringContextHolder;
import com.cntaiping.royal.bass.system.entity.SysReport;
import com.cntaiping.royal.bass.system.entity.SysResource;
import com.cntaiping.royal.bass.system.entity.SysRole;
import com.cntaiping.royal.bass.system.entity.SysUser;
import com.cntaiping.royal.bass.system.service.RedisUtil;
import com.cntaiping.royal.bass.system.service.SysReportService;
import com.cntaiping.royal.bass.system.service.SysResourceService;
import com.cntaiping.royal.bass.system.service.SysRoleService;
import com.cntaiping.royal.bass.system.service.SysUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户工具类
 *
 * @author ThinkGem
 * @version 2013-12-05
 */
public class UserUtils {

    private static SysUserService userService = SpringContextHolder.getBean(SysUserService.class);
    private static SysRoleService roleService = SpringContextHolder.getBean(SysRoleService.class);
    private static SysResourceService resourceService = SpringContextHolder.getBean(SysResourceService.class);
    private static SysReportService reportService = SpringContextHolder.getBean(SysReportService.class);
    private static RedisUtil redisService = SpringContextHolder.getBean(RedisUtil.class);

    public static final String USER_CACHE = "userCache";
    public static final String USER_CACHE_ID_ = "id_";
    public static final String USER_CACHE_LOGIN_NAME_ = "ln";
//	public static final String USER_CACHE_LIST_BY_OFFICE_ID_ = "oid_";

    public static final String CACHE_ROLE_LIST = "roleList";
    public static final String CACHE_MENU_LIST = "menuList";
    public static final String CACHE_REPORT_LIST = "reportList";
//	public static final String CACHE_AREA_LIST = "areaList";
//	public static final String CACHE_OFFICE_LIST = "officeList";
//	public static final String CACHE_OFFICE_ALL_LIST = "officeAllList";

    /**
     * 根据ID获取用户
     *
     * @param id
     * @return 取不到返回null
     */
    public static SysUser get(Long id) {
//		redisService.remove(USER_CACHE, USER_CACHE_ID_ + id);
		SysUser user = (SysUser)redisService.get(USER_CACHE_ID_ + id);
//        SysUser user = userService.queryById(id);
        if (user == null) {
            user = userService.queryById(id);
            if (user == null) {
                return null;
            }
            user.setRoleList(roleService.queryList(new SysRole(user)));
//			CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user);
//			CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getAccount(), user);
        }
        return user;
    }

    /**
     * 清除当前用户缓存
     */
    public static void clearCache() {
//		removeCache(CACHE_ROLE_LIST);
//		removeCache(CACHE_MENU_LIST);

        UserUtils.clearCache(getUser());
    }

    /**
     * 清除指定用户缓存
     *
     * @param user
     */
    public static void clearCache(SysUser user) {
//		CacheUtils.remove(USER_CACHE, USER_CACHE_ID_ + user.getId());
//		CacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getAccount());
    }

    /**
     * 获取当前用户
     *
     * @return 取不到返回 new User()
     */
    public static SysUser getUser() {
        SysUser principal = userService.queryById(0l);
        if (principal != null) {
            SysUser user = get(principal.getId());
            if (user != null) {
                return user;
            }
            return new SysUser();
        }
        // 如果没有登录，则返回实例化空的User对象。
        return new SysUser();
    }

    /**
     * 获取当前用户角色列表
     *
     * @return
     */
    public static List<SysRole> getRoleList() {
        @SuppressWarnings("unchecked")
        //		List<SysRole> roleList = (List<SysResource>)getCache(CACHE_MENU_LIST);
                List<SysRole> roleList = null;
        if (roleList == null) {
            SysUser user = getUser();
            if (user.isAdmin()) {
                roleList = roleService.findAllList(new SysRole());
            } else {
                SysRole role = new SysRole();
                role.setSysUser(user);
                roleList = roleService.queryList(role);
            }
//			putCache(CACHE_ROLE_LIST, roleList);
        }
        return roleList;
    }

    /**
     * 获取当前用户授权菜单
     *
     * @return
     */
    public static List<SysResource> getMenuList() {
//		redisService.remove(CACHE_MENU_LIST);
		List<SysResource> menuList = (List<SysResource>)redisService.get(CACHE_MENU_LIST);
        if (menuList == null) {
            SysUser user =  userService.queryById(2L);
            if (user.isAdmin()) {
                menuList = resourceService.findAllList(new SysResource());
            } else {
                SysResource m = new SysResource();
                m.setUserId(user.getId());
                menuList = resourceService.findByUserId(m);
            }
			redisService.set(CACHE_MENU_LIST, menuList);
        }
        return menuList;
    }

    /**
     * 获取当前用户授权报表
     *
     * @return
     */
    public static List<SysReport> getReportList() {
//		removeCache(CACHE_MENU_LIST);
//		List<SysReport> reportList = (List<SysReport>)getCache(CACHE_REPORT_LIST);
        List<SysReport> reportList = null;
        if (reportList == null) {
            SysUser user = getUser();
            if (user.isAdmin()) {
                reportList = reportService.queryListByReport(new SysReport());
            } else {
                SysReport p = new SysReport();
                p.setUserId(user.getId());
                reportList = reportService.findByUserId(p);
            }
//			putCache(CACHE_REPORT_LIST, reportList);
        }
        return reportList;
    }

    /**
     * 获取子菜单
     *
     * @return
     */
    public static List<SysResource> getSubMenuList(Long id) {
//		List<SysResource> menuList = (List<SysResource>)getCache(CACHE_MENU_LIST);
        List<SysResource> menuList = null;
        if (menuList == null) {
            menuList = getMenuList();
        }
        List<SysResource> subMenuList = new ArrayList<SysResource>();
        for (SysResource menu : menuList) {
            if (menu.getParentId() == id) {
                subMenuList.add(menu);
            }
        }
        return subMenuList;
    }

    public static Object getCache(String key, Object defaultValue) {
//		Object obj = getCacheMap().get(key);
        Object obj = userService.queryById(Long.decode(key));
        return obj == null ? defaultValue : obj;
    }
}
